package com.ebidding.controller;

import com.ebidding.entity.Orders;
import com.ebidding.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * RestController 是 Controller 及 ResponseBody
 */
@Controller
public class OrderController {


    @Autowired
    private OrdersService ordersService;

    /**
     * 到新增订单页面
     * @param mode
     * @return
     */
    @RequestMapping("/addOrder")
    public ModelAndView showCreateOrder(Model mode){
        return new ModelAndView("addOrder");
    }


    /**
     * 查询单个订单信息
     * @return
     */
    @RequestMapping("/find")
    public Orders findOrder(@RequestParam("orderId") String orderId){
        Orders order = ordersService.findOneOrder(orderId);
        return order;
    }

    /**
     * 查询所有订单信息
     * @return
     */
    @RequestMapping("/queryOrder")
    public ModelAndView findOrders(Model mode){
        List<Orders>  ordersList = ordersService.findAllOrders();
        mode.addAttribute("ordersList",ordersList);
        return new ModelAndView("queryOrder");
    }
    /**
     * 创建订单
     * @return
     * @param
     *//*
    @RequestMapping("/createOrder")
    public void createOrder(@RequestParam("goodsId") String goodsId,@RequestParam("price") Double price,
                            @RequestParam("number") Integer number,@RequestParam("userId") String userId,
                            @RequestParam("userName") String userName){
        Orders order = new Orders();
        //生成订单编号
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String orderId = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
        order.setOrderId(orderId);
        order.setGoodsId(goodsId);
        order.setNumber(number);
        order.setPrice(price);
        order.setUserId(userId);
        order.setUserName(userName);
        order.setCreatedTime(new Date());
        order.setCreatedDate(new Date());

        ordersService.createOrders(order);
    }*/

    /**
     * 创建订单
     * @param
     */
    @RequestMapping("/createOrder")
    public ModelAndView createOrder(Orders order){
         //生成订单编号
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String orderId = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
        order.setOrderId(orderId);
        order.setCreatedTime(new Date());
        order.setCreatedDate(new Date());

        ordersService.createOrders(order);
        return new ModelAndView("redirect:queryOrder");
    }
    /**
     * 到修改菜单页面
     * restful 风格
     */
    @RequestMapping("/toModify/{orderId}")
    public ModelAndView toModifyPage(@PathVariable("orderId") String orderId, Model model){
        model.addAttribute("order",ordersService.findOneOrder(orderId));
        return  new ModelAndView("/modifyOrder");
    }

    /**
     * 修改订单
     * @return
     * {
     *     orderId: 1,
     *     goodsId: 2,
     *     xxxx: xxx
     * }
     */
    @RequestMapping("/modifyOrder")
    public ModelAndView modifyOrder(Orders order){

        order.setUpdatedTime(new Date());

        ordersService.modifyOrders(order);
        return new ModelAndView("redirect:queryOrder");
    }

    /**
     * 删除订单
     * @return
     */
    @RequestMapping("/deleteOrder/{orderId}")
    public ModelAndView deleteOrder(@PathVariable("orderId")  String orderId){
        ordersService.deleteOrders(orderId);
        return new ModelAndView("redirect:/queryOrder");
    }
    //////////
}
